<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 用户相关控制器
*/
class UserController extends CommonController{
	public function index(){
		session('page_size',$_GET['page_size']);
        $search = $_GET['user_name'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("user");
        $where['user_name'] = array('like','%'.$search.'%');
        $where['del'] = '0';
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();
        $count = $MODEL->where($where)->count();
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        //dump($a);exit();
        $this->display();
    }
    public function user_list(){
        session('page_size',$_GET['page_size']);
        $search = $_GET['user_name'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("user");
        $where['username'] = array('like','%'.$search.'%');
        $where['del'] = '0';
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();

        foreach ($data as $key => $value) {
            if($value['sex'] == 1){
                $data[$key]['sex'] = '男';
            }elseif ($value['sex'] == 2) {
                $data[$key]['sex'] = '女';  
            }else{
                $data[$key]['sex'] = '未知';  
            }

            $data[$key]['is_verify'] = ($value['is_verify'] == 1) ? '已认证' : '未认证';
        }
        $count = $MODEL->where($where)->count();
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        //dump($a);exit();
        $this->display();
    }
    public function edit(){
        $this->display();
	}
    public function add(){
        $this->display();
    }


    //用户信息详情
    public function updata(){
        $user_id = I('id');
        if(!$user_id){
            $this->error('参数错误');
        }
        $data = M('user')->where('id='.$user_id)->find();

        if(!$data){
            $this->error('参数错误');
        }
        $this->assign('data',$data);
        $this->display();
    }

    //用户照片
    public function user_photos(){
        $user_id = I('user_id');
        if(!$user_id){
            $this->error('参数错误');
        }
        $photos = M('photos')->field("photo_id")->where('user_id='.$user_id)->select();
        $this->assign('photos',$photos);
        $this->display();
    }


    public function frize(){

        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;

        $data = M('today_times a')->field('a.user_id,a.status,b.username,b.phone,c.bao_times,c.abolish_times,c.tui_times')
            ->join('left join zm_user b on b.id=a.user_id')
            ->join('left join zm_frize c on a.user_id=c.user_id')
            ->select();
         $count = M("today_times")->count();
        foreach ($data as $key => $value) {
            
        }
         //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        //dump($a);exit();
        $this->display();
    }


    // 提现 

    public function money(){
        session('page_size',$_GET['page_size']);
        $search = $_GET['payname'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("bank_money");
        $where['payname'] = array('like','%'.$search.'%');
        if($_GET['status'] == 1){
            $where['status'] = array('neq',0);
        }
        if($_GET['status'] == 2){
            $where['status'] = 0;
        }
        // $where['del'] = '0';
        // dump($search);exit();
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();

        foreach ($data as $key => $value) {
            if($value['status'] == 0){
                $data[$key]['statusn'] = '新增';
            }elseif ($value['status'] == 1) {
                $data[$key]['statusn'] = '通过';  
            }else{
                $data[$key]['statusn'] = '驳回';  
            }

            $data[$key]['is_verify'] = ($value['is_verify'] == 1) ? '已认证' : '未认证';
        }
        $count = $MODEL->where($where)->count();
        $this->assign("search", $search);
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        //dump($a);exit();
        $this->display();
    }

    // 通过驳回
    public function ispass(){
        $id = I('get.id');
        $ispass = I('ispass');

        $user = M('bank_money')->where("id='$id'")->find();
        if (!$user) 
        {
            $this->error('参数错误');
        }

        $data = M('bank_money')->where("id='$id'")->save(array('status'=>$ispass,'srue_time'=>time()));

        if ($data) 
        {
            if ($ispass == 1) 
            {   
                $content = '提现成功！提现金额将在2到7天到账！';
                $extra = time();
                $ds=system_to_one($user['user_id'],$content,$extra);
                A('Message')->addSysMsg($user['user_id'],$content);
                // dump($ds);exit();
            }

            if ($ispass == 2) {
                $content = '提现被驳回！';
                $extra = time();
                system_to_one($user['user_id'],$content,$extra);
                A('Message')->addSysMsg($user['user_id'],$content);
                M('user')->where(array('id'=>$user['user_id']))->setInc('money',$user['money']);

            }
            $this->success('审核成功！');
        }else{
            $this->error('网络异常，请重试！');
        }

    }

    public function mass_pass(){
        $ids = json_decode($_POST['ids'],true);
        $type = I('type') ? I('type') : 1;
        if(empty($ids)){
            $this->ajaxReturn(array('status'=>0,'msg'=>'请先选择需要处理的数据'));
        }
        $where['id'] = array('in',$ids);
        $where['status'] = 0;
        $MODEL = M('bank_money');
        $list = $MODEL->where($where)->select();
        if(count($list) != count($ids)){
            $this->ajaxReturn(array('status'=>0,'msg'=>'重复处理'));
        }
        $where['status'] = 0;
        $MSG_MODEL = A('Message');
        if($type == 1){
            $content = '提现成功！提现金额将在2到7天到账！';
            $res = $MODEL->where($where)->save(array('status'=>1,'srue_time'=>time()));
            
        }else{
            $content = '提现被驳回！';
            $res = $MODEL->where($where)->save(array('status'=>2,'srue_time'=>time()));
            foreach ($list as $key => $value) {
                M('user')->where(array('id'=>$value['user_id']))->setInc('money',$value['money']);
            }
        }
        if($res ===false){
            $this->ajaxReturn(array('status'=>1,'msg'=>'处理失败'));
        }
        foreach ($list as $key => $value) {
            $MSG_MODEL->addSysMsg($value['user_id'],$content);
        }
        $this->ajaxReturn(array('status'=>1,'msg'=>'处理成功'));
    }

    public function tip(){
        session('page_size',$_GET['page_size']);
        $search = $_GET['payname'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("tip");
        $where['payname'] = array('like','%'.$search.'%');
        // $where['del'] = '0';
        // dump($search);exit();
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();
        foreach ($data as $key => $value) {
            // if($value['status'] == 0){
            //     $data[$key]['statusn'] = '新增';
            // }elseif ($value['status'] == 1) {
            //     $data[$key]['statusn'] = '通过';  
            // }else{
            //     $data[$key]['statusn'] = '驳回';  
            // }
            $data[$key]['image'] = unserialize($value['image']);  
        }

        $count = $MODEL->where($where)->count();
        $this->assign("search", $search);
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        //dump($a);exit();
        $this->display();
    }

    public function my_tui(){
        $user_id = I('user_id');
        if(!$user_id){
            $this->error('参数错误');
        }
        $data = M('user')->where('tui_id='.$user_id)->select();
        $username = M('user')->where('id='.$user_id)->getField('username');
        $total = 0;
        if($data){
            $total = count($data);
        }

        $this->assign('data',$data);
        $this->assign('count',$total);
        $this->assign('username',$username);
        $this->display();
    }
}